package com.example.yls.demoa;

import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

public class SelectActivity extends AppCompatActivity implements View.OnClickListener{

    private Button s_delete, s_back;
    private ImageView s_img;
    private VideoView s_video;
    private TextView s_tv;
    private Notesdb notesdb;
    private SQLiteDatabase dbwriter;
    /** 点击item 的详情页 对数据进行删除操作*/
    @Override
    protected void onCreate(Bundle savedinstancestate) {
        super.onCreate(savedinstancestate);
        setContentView(R.layout.activity_select);

        s_delete = (Button) findViewById(R.id.s_delete);
        s_back = (Button) findViewById(R.id.s_back);
        s_img = (ImageView) findViewById(R.id.s_img);
        s_video = (VideoView) findViewById(R.id.s_video);
        s_tv = (TextView) findViewById(R.id.s_tv);
        /**读写权限*/
        notesdb = new Notesdb(this);
        dbwriter = notesdb.getWritableDatabase();

        s_back.setOnClickListener(this);
        s_delete.setOnClickListener(this);
        //判断是否存在图片
        if (getIntent().getStringExtra(notesdb.path).equals("null")) {
            s_img.setVisibility(View.GONE);
        } else {
            s_img.setVisibility(View.VISIBLE);
        }

        if (getIntent().getStringExtra(notesdb.video).equals("null")) {
            s_video.setVisibility(View.GONE);
        } else {
            s_video.setVisibility(View.VISIBLE);
        }
        // 设置需要显示的图片
        s_tv.setText(getIntent().getStringExtra(notesdb.content));

        Bitmap bitmap = BitmapFactory.decodeFile(getIntent().getStringExtra(
                notesdb.path));
        s_img.setImageBitmap(bitmap);

        s_video.setVideoURI(Uri
                .parse(getIntent().getStringExtra(notesdb.video)));

        s_video.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.s_delete:
                deletedate();
                finish();
                break;

            case R.id.s_back:
                finish();
                break;
        }
    }
    //数据的删除
    public void deletedate() {
        dbwriter.delete(notesdb.table_name,
                "_id=" + getIntent().getIntExtra(notesdb.id, 0), null);
    }

}